package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.UserGroupDto;

/**
 * 用户组
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UserGroupJdbcDao implements UserGroupDao {
	@Override
	public int deleteByIds(final UserGroupDto deleteUserGroup) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUserGroup.getAttrs())) {
			model.putAll(deleteUserGroup.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUserGroup.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UserGroupDto findUserGroup) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUserGroup, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUserGroup, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUserGroup.getAttrs())) {
			model.putAll(findUserGroup.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UserGroupDto findUserGroup) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUserGroup);

		final Model model = ModelUtils.newModel(new ModelArgs(findUserGroup, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUserGroup.getAttrs())) {
			model.putAll(findUserGroup.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UserGroupDto findListUserGroup) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUserGroup).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUserGroup.getAttrs())) {
			model.putAll(findListUserGroup.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UserGroupDto insertUserGroup) {
		if (StringUtils.isBlank(insertUserGroup.getId())) {
			insertUserGroup.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUserGroup));

		if (MapUtils.isNotEmpty(insertUserGroup.getAttrs())) {
			model.putAll(insertUserGroup.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UserGroupDto paginationUserGroup) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUserGroup).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUserGroup.getAttrs())) {
			model.putAll(paginationUserGroup.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUserGroup), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UserGroupDto readUserGroup) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUserGroup, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUserGroup.getAttrs())) {
			model.putAll(readUserGroup.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUserGroup);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUserGroup, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UserGroupDto saveUserGroup) {
		final P oldUserGroup = this.findByCodeOrId(mappedClass, saveUserGroup);

		if (oldUserGroup == null) {
			this.insertByCode(saveUserGroup);

			return this.findByCodeOrId(mappedClass, saveUserGroup);
		}

		final int updateCount = this.updateByCode(saveUserGroup, oldUserGroup);

		if (updateCount < 1) {
			return oldUserGroup;
		}

		return this.findByCodeOrId(mappedClass, saveUserGroup);
	}

	@Override
	public int updateByCode(final UserGroupDto updateUserGroup) {
		final UserGroupDto oldUserGroup = this.findByCodeOrId(UserGroupDto.class, updateUserGroup);

		return this.updateByCode(updateUserGroup, oldUserGroup);
	}

	private <P> int updateByCode(final UserGroupDto updateUserGroup, final P oldUserGroup) {
		if (oldUserGroup == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUserGroup);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUserGroup, oldUserGroup, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUserGroup, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUserGroup, updateUserGroup, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUserGroup, updateProperties), new ModelArgs(updateUserGroup, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUserGroup.getAttrs())) {
			model.putAll(updateUserGroup.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}